home *** CD-ROM | disk | FTP | other *** search
/ Windows 32-Bit Gallery / Windows 32-bit Gallery.iso / win95 / winbatch / meter.wb_ < prev    next >
Text File  |  1996-11-20  |  4KB  |  121 lines

  1. ; Program: METER.WBT  -  Author: Richard Merit
  2.     Message("Information","METER.WBT shows how to meter network software  Inspect file for more information.")
  3.     exit
  4.  
  5. ;-----------------------------
  6. meterdir = "c:\temp"       ; Should be the directory on the network drive
  7. ;-----------------------------
  8.  
  9.  
  10. meterok = @NO              ; Default case is that the app is maxed out
  11.  
  12. ; Parameter Validation.  Needs two or three params
  13. ; Param1 = Applications Name
  14. ; Param2 = ADDUSER or REMOVEUSER
  15. ; Param3 = User Name
  16. If (param0 < 2) || (param0 > 3) Then Goto parmerror
  17.  
  18. ;Make sure the meter directory has a whack on the end of it
  19. If StrSub(meterdir, StrLen(meterdir), 1) != "\" Then meterdir = StrCat(meterdir, "\")
  20.  
  21. ; Use appname to build app ini file name
  22. appname = StrUpper(param1)
  23. inifile = StrUpper("%meterdir%%appname%.ini")
  24. If FileExist(inifile) == @FALSE Then Goto noini
  25.  
  26. ; And build a "LockFile" name, used to prevent simultaneous access to the ini file
  27. lockfile = StrUpper("%meterdir%%appname%.lok")
  28.  
  29. ; Build username parameter
  30. username = "UNKNOWN"
  31. If param0 == 3 Then username = param3
  32.  
  33. ; So are we adding a user or removing a user
  34.  
  35. If StriCmp(param2, "ADDUSER") == 0 Then Goto increase
  36. If StriCmp(param2, "REMOVEUSER") == 0 Then Goto decrease
  37. Goto parmerror
  38.  
  39. :increase
  40. ; Assume responsibility for error handling, and obtain a Lock
  41. ; on the resources via the LockFile
  42. ErrorMode(@OFF)
  43. :iretry
  44. handle = FileOpen(lockfile, "WRITE")        ; Attempt exclusive access at lock
  45. If LastError() == 1077 then  Goto iretry    ; Failed?  Try again
  46. ErrorMode(@CANCEL)
  47.  
  48. ; Get max count
  49. maxusers = IniReadPvt(appname, "Maximum Users", "", inifile)
  50. If maxusers == "" Then Goto noini
  51.  
  52. ; No user counts for unknown users
  53. if username!="UNKNOWN" then goto checkusage
  54.  
  55. ; Is a copy already checked out to this user?
  56. userstatus = IniReadPvt(appname, username, 0, inifile)
  57. If userstatus > 0  Then Goto allowuser       ; Yes, do not increment usage counts
  58.  
  59. :checkusage
  60. ; How many people using this now
  61. currusers = IniReadPvt(appname, "Current Users", 0, inifile)
  62. If currusers >= maxusers Then Goto userlimit         ; Too many users
  63.  
  64. ; Increment usage count
  65. IniWritePvt(appname, "Current Users", currusers + 1, inifile)
  66. ; And remember who is active
  67. If username != "UNKNOWN" Then IniWritePvt(appname, username, userstatus+1, inifile)
  68.  
  69. :allowuser
  70. ; Release Lock
  71. FileClose(handle)
  72. ; Return go-ahead signal
  73. meterok = @YES
  74. Return
  75. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  76. :decrease
  77. ErrorMode(@OFF)
  78.  
  79. :dretry
  80. ; Assume responsibility for error handling, and obtain a Lock
  81. ; on the resources via the LockFile
  82. ErrorMode(@OFF)
  83. :iretry
  84. handle = FileOpen(lockfile, "WRITE")        ; Attempt exclusive access at lock
  85. If LastError() == 1077 then  Goto dretry    ; Failed?  Try again
  86. ErrorMode(@CANCEL)
  87.  
  88. ; If no username, just reduce the usage count
  89. if username=="UNKNOWN" then goto reduceeusage
  90.  
  91. userstatus = IniReadPvt(appname, username, 1, inifile)
  92. newstatus=userstatus-1
  93. if newstatus>=0 then IniWritePvt(appname, username, newstatus, inifile)
  94.  
  95. if newstatus != 0 then goto dexit
  96.  
  97. ; Get Count of current users
  98. :reduceusage
  99. currusers = IniReadPvt(appname, "Current Users", 0, inifile)
  100. If currusers > 0 Then IniWritePvt(appname, "Current Users", currusers - 1, inifile)
  101.  
  102. :dexit
  103. FileClose(handle)
  104. Return
  105.  
  106. :userlimit
  107. FileClose(handle)
  108. Return
  109.  
  110. :parmerror
  111. Message("METER Error", "Invalid usage")
  112. Return
  113.  
  114. :noini
  115. IniWritePvt(appname, "Maximum Users", 0, inifile)
  116. IniWritePvt(appname, "Current Users", 0, inifile)
  117. If IsDefined(handle) == @YES Then FileClose(handle)
  118. Message("METER Error", "Usage limit for %appname% is unknown")
  119. If FileExist(inifile) == @FALSE Then Message("METER Error", "Could not create %inifile%")
  120. Return
  121.